package com.zl.controller;

import com.zl.pojo.User;
import com.zl.service.BankService;
import com.zl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author ljm
 * @Date 2021/11/23 11:35
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService us;
    @Autowired
    private BankService zz;
    @RequestMapping("/login")
    public String login(User loginUser, String isno, HttpSession session, HttpServletResponse resp) {
        try{
            loginUser = us.login(loginUser);
            if(loginUser != null){
                if("yes".equals(isno)){
                    //创建cookie对象
                    Cookie cName = new Cookie("name", URLEncoder.encode((loginUser.getName()),"utf-8"));
                    Cookie cPwd=new Cookie("pwd",loginUser.getPwd());
                    //设置cookie的有效时间
                    cName.setMaxAge(7*24*60*60);
                    cPwd.setMaxAge(7*24*60*60);
                    //设置cookie根路径
                    cName.setPath("/empMaven");
                    cPwd.setPath("/empMaven");
                    resp.addCookie(cName);
                    resp.addCookie(cPwd);
                }
                session.setAttribute("loginUser",loginUser);
                return "redirect:/emp/toMain";
            }else {
                return "redirect:/login.jsp?error="+URLEncoder.encode("用户名或者密码错误","utf-8");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/out")
    public String out(HttpSession session){
        session.invalidate();// 注销session
        return "redirect:/login.jsp";
    }

    @RequestMapping("/regName")
    @ResponseBody//告诉springMVC需要把返回值转换为json格式
    public Map<String,Object> regName(String name){
        Map<String,Object> result = new HashMap<>();
        int num = us.regName(name);
        if (num > 0) {
            result.put("result",false);
        } else {
            result.put("result",true);
        }
        return result;
    }

    @RequestMapping("/regUser")// 请求路径被拦截
    public String regUser(User user, HttpServletRequest req){
        User user1 = us.queryUserByName(user.getName());
        if (user1 == null) {
            us.regUser(user);
        } else {
            System.out.println("该用户已存在！注册失败");
            req.setAttribute("flag",false);
        }
        return "redirect:/login.jsp";
    }
    @RequestMapping("/zz")
    public String zz(){
        zz.zz();
        return "redirect:/emp/toMain";
    }
}

